<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1200" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">

					<el-collapse v-model="activeNames">
						<el-collapse-item title="基础设置" name="基础设置">

							<template #title>
								<div class="class_title">1.基础设置</div>
							</template>

							<el-form-item style="margin-top: 18px;" label="模板分类" prop="category_id">
								<blCategory style="width: 400px;" v-model="form.category_id" model="WxWorkTemplate" />
							</el-form-item>
							<el-form-item label="模板名称" prop="wxwork_name">
								<el-input v-model="form.wxwork_name" style="width: 400px;"></el-input>
							</el-form-item>
							<el-form-item label="模板内容" prop="wxwork_content">
								<!--
								<el-input type="textarea" style="width: 500px;" v-model="form.wxwork_content" :rows="6"
									placeholder="请输入内容" show-word-limit maxlength="134"></el-input>
								-->
								<sc-editor v-model="form.wxwork_content" placeholder="请输入内容" :height="600"></sc-editor>
							</el-form-item>
							<el-form-item label="短信平台" prop="wxwork_platform">
								<el-radio-group style="width: 400px;" v-model="form.wxwork_platform">
									<el-radio-button label="南宁柏乐口腔医院有限公司">南宁柏乐口腔医院有限公司</el-radio-button>
								</el-radio-group>
							</el-form-item>
							<el-form-item label="审核状态" prop="wxwork_status">
								<el-radio-group v-model="form.wxwork_status">
									<el-radio :label="'审核通过'">审核通过</el-radio>
									<el-radio :label="'审核驳回'">审核驳回</el-radio>
									<el-radio :label="'待审核'">待审核</el-radio>
									<el-radio :label="'审核中'">审核中</el-radio>
								</el-radio-group>
							</el-form-item>

						</el-collapse-item>


					</el-collapse>

					<div style="height:400px">
						&nbsp;
					</div>

				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				apiObj: {},
				addTemplate: {

				},
				shortcuts: [{
					text: "每天0点5分",
					value: "0 5 0 * * ?"
				}],
				activeNames: ['基础设置', '高级设置'],
				props: {
					label: 'title',
					value: 'id',
					keyword: 'keyword'
				},
				activeTab: 'basic',
				data: {

				},
				loading: false,
				mode: "add",
				titleMap: {
					add: '添加企微消息模板',
					edit: '编辑企微信消息模板'
				},
				form: {
					id: '',
					wxwork_name: '',
					wxwork_content: '',
					wxwork_status: '待审核',
					wxwork_platform: '南宁柏乐口腔医院有限公司',
					category_id: '',
				},
				rules: {
					wxwork_name: [{
						required: true,
						message: '请填写名称'
					}],
					wxwork_content: [{
						required: true,
						message: '请填写内容'
					}],
					wxwork_status: [{
						required: true,
						message: '请选择审核状态'
					}],
					wxwork_platform: [{
						required: true,
						message: '请选择企微账号'
					}]
				},
				visible: false,
				isSaveing: false,
				apiObj: null,
			}
		},
		mounted() {

		},
		methods: {
			success(res) {
				return true;
			},
			//显示
			open(data) {

				this.apiObj = this.$parent.apiObj

				if (data) {
					this.mode = data;
				}
				this.visible = true;
				this.$nextTick(() => {
					if (this.mode == 'add') {
						//this.$refs.table.rowAdd()
					}
				})
				return this;
			},
			//表单提交方法
			submit() {
				this.$refs.ruleForm.validate(async (data) => {
					if (data) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'add') {
							res = await this.apiObj.create.post(this.form);
						} else {
							res = await this.apiObj.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', data, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {

				this.loading = true

				if (!data.id) {
					if (data.category_id) {
						this.form.category_id = data.category_id
					}
					this.loading = false
					return;
				}

				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.apiObj.show.get(params)
					this.loading = false
					this.form = res.data
				}, 0)
			}
		}
	}
</script>

<style>
	.class_title {
		padding-left: 18px;
		font-size: 18px;
		background-color: #f4f9ff;
		text-align: left;
		width: 100%;
	}
</style>
